package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.RoomBean;
import com.project.bean.StudentBean;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IRoomSetMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class RoomServiceImpl implements IRoomService {
    @Autowired
    private IRoomMapper roomMapper;
    @Autowired
    private IStudentMapper studentMapper;
    @Autowired
    private IRoomSetMapper roomSetMapper;

    @Override
    public void add(RoomBean room) {
        roomMapper.insert(room);
    }

    @Override
    public IPage<RoomBean> findByItem(int pageNO,String address, String status, String isIn, String type) {
        return roomMapper.findByItem(new Page(pageNO,3),address,
                status,isIn,type);
    }

    @Override
    public RoomBean findById(int id) {
        RoomBean room = roomMapper.findById(id);
        room.setStudentList(studentMapper.findByRoom(id));
        return room;
    }

    @Override
    public boolean del(int id) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("fk_roomId",id);
        //统计房间人数
        Integer stuNum = studentMapper.selectCount(qw);
        if(stuNum != 0){
            return false;
        }
        roomSetMapper.delete(qw);
        roomMapper.deleteById(id);

        return true;
    }
}
